package com.haiyangwang1.cainiao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.Scanner;

public class NormalFrame {
    Customer[] customers = new Customer[10];
    Employee[] e1 = new Employee[10];
    JFrame normalFrame = new JFrame();
    private JTextArea area;
    private JTextField txtInput;
    private JButton btn, btn1;
    private JPanel p1;
    private boolean isFlag = false;
    private int index = 0;

    public NormalFrame() {
        initData();

        normalFrame.setBounds(400, 200, 400, 300);
        Scanner input = new Scanner(System.in);
        //上面的输入框
        area = new JTextArea();
        area.setText("欢迎进入职员系统\n\n 1.入库\n2.客户取货\n3.查看未收货订单\n4.按取货码查询订单\n5.按姓名查询订单\n6.修改密码\n7.安全退出");
        //下面的输入框和按钮放在一个容器里面
        p1 = new JPanel();
        txtInput = new JTextField(20);
        btn = new JButton("确定");
        btn1 = new JButton("取消");
        p1.add(txtInput);
        p1.add(btn);
        p1.add(btn1);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = txtInput.getText();
                switch (str) {
                    case "1":
                        into();
                        break;
                    case "2":
                        takegoods();
                        break;
                    case "3":
                        look();
                        break;
                    case "4":
                        expressnum();
                        break;
                    case "5":
                        takename();
                        break;
                    case "6":
                        reset();
                        break;
                    case "7":
                        returnMain();
                        break;
                }
            }
        });
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //把容器和上面的输入框加到窗口里面
        normalFrame.add(area);
        normalFrame.add(p1, BorderLayout.SOUTH);
        normalFrame.setVisible(true);
        normalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    //安全退出
    public void returnMain() {
        normalFrame.dispose();

    }

    //重置密码
    private void reset() {
        String oldpwd;
        oldpwd = JOptionPane.showInputDialog(null, "请输入你的旧密码：");
        for (int i = 0; i < index; i++) {
            if (e1[i].getPassword().equals(oldpwd)) {
                String newpwd = JOptionPane.showInputDialog(null, "请输入你的新密码：");
                String newpwd1 = JOptionPane.showInputDialog(null, "请再次输入你的新密码：");
                if (newpwd.equals(newpwd1)) {
                    e1[i].setPassword(newpwd);
                } else {
                    JOptionPane.showMessageDialog(null, "两次密码不一致");
                }
            } else {
                JOptionPane.showMessageDialog(null, "旧密码错误");

            }
        }
    }

    //按姓名查看信息
    private void takename() {
        String takename = JOptionPane.showInputDialog(null, "请输入收货人姓名：");
        for (int i = 0; i <= index; i++) {
            if (customers[i].getConsignee().equals(takename)) {
                String str1 = "取货码    收货人    收货人电话    入库时间     取货时间     取货人     经办人\n";

                Customer customer = customers[i];
                str1 += customer.getGetExpressNum() + "        " + customer.getConsignee() + "        " + customer.getGetConsigneePhone() + "    " + LocalDate.now() + "   " + customer.getOutTime() + "        " + customer.getTakeGoodsPeople() + "\n";
                JOptionPane.showMessageDialog(null, str1);
            } else if (i == index - 1) {
                JOptionPane.showMessageDialog(null, "该收货人不存在");
                break;
            }
//            break;
//            }else {
//                JOptionPane.showMessageDialog(null,"该收货人不存在");
//                break;
//            }

        }
    }

    //按取货码查看信息
    private void expressnum() {
        String take = JOptionPane.showInputDialog(null, "请输入取货码：");
        for (int i = 0; i < customers.length; i++) {
            if (customers[i].getGetExpressNum().equals(take)) {
                String str1 = "取货码    收货人    收货人电话    入库时间     取货时间     取货人     经办人\n";

                Customer customer = customers[i];
                str1 += customer.getGetExpressNum() + "        " + customer.getConsignee() + "        " + customer.getGetConsigneePhone() + "    " + LocalDate.now() + "   " + customer.getOutTime() + "        " + customer.getTakeGoodsPeople() + "\n";
                JOptionPane.showMessageDialog(null, str1);

            } else if (i == index - 1) {
                JOptionPane.showMessageDialog(null, "该取货码不存在");
                break;
            }
//            break;
        }

    }

    //取货
    private void takegoods() {
        String take = JOptionPane.showInputDialog(null, "请输入取货码：");

        for (int i = 0; i < index; i++) {
            if (customers[i].getGetExpressNum().equals(take)) {
                //取货码存在，然后显示未被取货的货物
                isFlag = true;
            }
//                System.out.println("zhaodaole");
            if (customers[i].getOutTime().equals("未取货")) {
//                System.out.println("zhaole");
                isFlag = true;
            }
            if (isFlag) {
//                System.out.println("找到了");
                String str1 = "取货码    收货人    收货人电话    入库时间     取货时间     取货人     经办人\n";
                for (int j = 0; j < index; j++) {
                    Customer customer = customers[j];
                    str1 += customer.getGetExpressNum() + "        " + customer.getConsignee() + "        " + customer.getGetConsigneePhone() + "    " + LocalDate.now() + "   " + customer.getOutTime() + "        " + customer.getTakeGoodsPeople() + "\n";
                }
                JOptionPane.showMessageDialog(null, str1);
                JOptionPane.showConfirmDialog(null, "请问是否需要取出包裹");
                JOptionPane.showInputDialog(null, "取货人签字");
                index--;
            } else {
                JOptionPane.showMessageDialog(null, "快递已被取走");
            }
            break;
        }
    }

    private void into() {
        String expressnum = JOptionPane.showInputDialog(null, "请输入取货码：");

        String consignee = JOptionPane.showInputDialog(null, "请输入收货人：");
        String consigneephone = JOptionPane.showInputDialog(null, "请输入收货人电话：");

        if (index < customers.length) {

            customers[index] = new Customer(expressnum, consignee, consigneephone, LocalDate.now(), "未取货", "无");
            for (int i = 0; i < index; i++) {
                if (customers[i] != null) {
                    Customer customer = customers[index];
                    index++;
                    break;
                }
            }
        }
    }

    private void look() {
        String str1 = "取货码    收货人    收货人电话    入库时间     取货时间     取货人     经办人\n";


        for (int i = 0; i < index; i++) {

            Customer customer = customers[i];
            str1 += customer.getGetExpressNum() + "        " + customer.getConsignee() + "        " + customer.getGetConsigneePhone() + "    " + LocalDate.now() + "   " + customer.getOutTime() + "        " + customer.getTakeGoodsPeople() + "\n";

        }

        JOptionPane.showMessageDialog(null, str1);
    }

    public void initData() {

        customers[0] = new Customer("2856", "叶琛", "13528964561", LocalDate.now(), "未取货", "无");
        index++;
        customers[1] = new Customer("1529", "墨炔", "18565964561", LocalDate.now(), "未取货", "无");
        index++;
    }

}
